{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "293d3d9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 手淘流量(三级)\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 18)\n",
    "end_date = datetime(2023, 12, 20)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "        item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "        # 构建链接列表\n",
    "        urls = []\n",
    "        current_date = start_date\n",
    "        while current_date <= end_date:\n",
    "            formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "            urls.append(url)\n",
    "            current_date += timedelta(days=1)\n",
    "\n",
    "        # 批量打开链接\n",
    "        for url in urls:\n",
    "            webbrowser.open(url)\n",
    "\n",
    "        # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "        time.sleep(5)\n",
    "\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 批量复制下载好的表格到相应的子文件夹\n",
    "            for downloaded_file in downloaded_files:\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "\n",
    "                # 批量删除下载目录中的源文件\n",
    "                os.remove(downloaded_file)\n",
    "                print(f\"Removed source file: {downloaded_file}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "399da6c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单品流量(二级)\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 18)\n",
    "end_date = datetime(2023, 12, 20)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "        item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "        # 构建链接列表\n",
    "        urls = []\n",
    "        current_date = start_date\n",
    "        while current_date <= end_date:\n",
    "            formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id_variable}&device=2&order=desc&orderBy=uv\"\n",
    "            urls.append(url)\n",
    "            current_date += timedelta(days=1)\n",
    "\n",
    "        # 批量打开链接\n",
    "        for url in urls:\n",
    "            webbrowser.open(url)\n",
    "\n",
    "        # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "        time.sleep(5)\n",
    "\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 批量复制下载好的表格到相应的子文件夹\n",
    "            for downloaded_file in downloaded_files:\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "\n",
    "                # 批量删除下载目录中的源文件\n",
    "                os.remove(downloaded_file)\n",
    "                print(f\"Removed source file: {downloaded_file}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
